//
//  XMZhuCeViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/14.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "XMZhuCeViewController.h"
#import "EBCountDownButton.h"
#import "Masonry.h"
#import "GPosAPI.h"
#import "SVProgressHUD.h"
#import "WKProgressHUD.h"

@interface XMZhuCeViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField * phoneText;
@property (nonatomic,strong)UITextField * yzmText;
@property (nonatomic,strong)UITextField * passWordText;
@property (nonatomic,strong)UITextField * QrPassWordText;
@property (nonatomic,strong)EBCountDownButton * yzmButton;
@property(strong,nonatomic)WKProgressHUD *hud;
@property (nonatomic,strong)UITextField * yqmText;
@property (nonatomic,strong)UIButton * zhuceButton;
@property (nonatomic,strong)NSDictionary * para;

@end

@implementation XMZhuCeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    self.navigationController.navigationBar.hidden=NO;
    self.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    [self passWordLoadView];
    if (![self.phonedh isEqualToString:@""]) {
        self.phoneText.text = self.phonedh;
        return;
    }
}

- (void)passWordLoadView {
    
    UIImageView * sjImage = [[UIImageView alloc] init];
    [sjImage setImage:[UIImage imageNamed:@"icon_shouji"]];
    [self.view addSubview:sjImage];
    
    [sjImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(40);
        make.top.equalTo(self.view).with.offset(kNavbarHeight+36);
        
    }];
    
    self.phoneText = [[UITextField alloc] init];
    self.phoneText.placeholder =@"请输入手机号";
    self.phoneText.keyboardType=UIKeyboardTypeNumberPad;
    self.phoneText.font = [UIFont systemFontOfSize:14.f];
    //    self.phoneText.secureTextEntry = YES;
    self.phoneText.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.phoneText];
    [self.phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sjImage).with.offset(22);
        make.centerY.equalTo(sjImage);
        make.right.equalTo(self.view).with.offset(-40);
        
    }];
    
    UIView * phoneView = [[UIView alloc] init];
    phoneView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:phoneView];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(40);
        make.top.equalTo(sjImage.mas_bottom).with.offset(10);
        make.right.equalTo(self.view).with.offset(-40);
        make.height.equalTo(@0.5);
        
    }];
    
    UIView * yzmView = [[UIView alloc] init];
    yzmView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:yzmView];
    [yzmView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(40);
        make.top.equalTo(phoneView.mas_bottom).with.offset(60);
        make.right.equalTo(self.view).with.offset(-40);
        make.height.equalTo(@0.5);
        
    }];
    
    UIImageView * yzmImage = [[UIImageView alloc] init];
    [yzmImage setImage:[UIImage imageNamed:@"icon_yz"]];
    [self.view addSubview:yzmImage];
    
    [yzmImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(yzmView.mas_top).with.offset(-10);
        make.left.equalTo(self.view.mas_left).with.offset(40);
        
    }];
    
    self.yzmButton = [EBCountDownButton buttonWithType:UIButtonTypeCustom];
    [self.yzmButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.yzmButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.yzmButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:self.yzmButton];
    [self.yzmButton addTarget:self action:@selector(yzmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.yzmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(yzmImage);
        make.right.equalTo(self.view).with.offset(-40);
        make.width.equalTo(@75);
    }];
    
    self.yzmText = [[UITextField alloc] init];
    self.yzmText.placeholder =@"请输入验证码";
    self.yzmText.font = [UIFont systemFontOfSize:14.f];
    self.yzmText.keyboardType=UIKeyboardTypeNumberPad;
    //    self.phoneText.secureTextEntry = YES;
    self.yzmText.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.yzmText];
    [self.yzmText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yzmImage).with.offset(22);
        make.centerY.equalTo(yzmImage);
        make.right.equalTo(self.yzmButton.mas_left).with.offset(-10);
        
    }];
    
    UIView * xmimaView = [[UIView alloc] init];
    xmimaView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:xmimaView];
    [xmimaView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(40);
        make.top.equalTo(yzmView.mas_bottom).with.offset(60);
        make.right.equalTo(self.view).with.offset(-40);
        make.height.equalTo(@0.5);
        
    }];
    
    UIImageView * xmimaImage = [[UIImageView alloc] init];
    [xmimaImage setImage:[UIImage imageNamed:@"icon_mima"]];
    [self.view addSubview:xmimaImage];
    
    [xmimaImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(xmimaView.mas_top).with.offset(-10);
        make.left.equalTo(self.view.mas_left).with.offset(40);
        
    }];
    
    self.passWordText = [[UITextField alloc] init];
    self.passWordText.placeholder =@"请输入密码";
    self.passWordText.font = [UIFont systemFontOfSize:14.f];
    self.passWordText.secureTextEntry = YES;
    self.passWordText.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.passWordText];
    [self.passWordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(xmimaImage).with.offset(22);
        make.centerY.equalTo(xmimaImage);
        make.right.equalTo(self.view).with.offset(-40);
        
    }];
    
    UIView * qrmimaView = [[UIView alloc] init];
    qrmimaView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:qrmimaView];
    [qrmimaView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(40);
        make.top.equalTo(xmimaView.mas_bottom).with.offset(61);
        make.right.equalTo(self.view).with.offset(-40);
        make.height.equalTo(@0.5);
        
    }];
    
    UIImageView * qrmimaImage = [[UIImageView alloc] init];
    [qrmimaImage setImage:[UIImage imageNamed:@"icon_mima"]];
    [self.view addSubview:qrmimaImage];
    
    [qrmimaImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(qrmimaView.mas_top).with.offset(-10);
        make.left.equalTo(self.view.mas_left).with.offset(40);
        
    }];
    
    self.QrPassWordText = [[UITextField alloc] init];
    self.QrPassWordText.placeholder =@"请再次输入密码";
    self.QrPassWordText.font = [UIFont systemFontOfSize:14.f];
    self.QrPassWordText.secureTextEntry = YES;
    self.QrPassWordText.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.QrPassWordText];
    [self.QrPassWordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(qrmimaImage).with.offset(22);
        make.centerY.equalTo(qrmimaImage);
        make.right.equalTo(self.view).with.offset(-40);
        
    }];
    
    UIView * yqmView = [[UIView alloc] init];
    yqmView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:yqmView];
    [yqmView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(40);
        make.top.equalTo(qrmimaView.mas_bottom).with.offset(61);
        make.right.equalTo(self.view).with.offset(-40);
        make.height.equalTo(@0.5);
        
    }];
    
    UIImageView * yqmImage = [[UIImageView alloc] init];
    [yqmImage setImage:[UIImage imageNamed:@"icon_mima"]];
    [self.view addSubview:yqmImage];
    
    [yqmImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(yqmView.mas_top).with.offset(-10);
        make.left.equalTo(self.view.mas_left).with.offset(40);
        
    }];
    
    self.yqmText = [[UITextField alloc] init];
    self.yqmText.placeholder =@"请输入邀请码";
    self.yqmText.keyboardType=UIKeyboardTypeNumberPad;
    self.yqmText.font = [UIFont systemFontOfSize:14.f];
//    self.yqmText.secureTextEntry = YES;
    self.yqmText.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.yqmText];
    [self.yqmText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yqmImage).with.offset(22);
        make.centerY.equalTo(yqmImage);
        make.right.equalTo(self.view).with.offset(-40);
        
    }];
    
    self.zhuceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.zhuceButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.zhuceButton setBackgroundImage:[UIImage imageNamed:@"icon_anniu_c"] forState:UIControlStateNormal];
    [self.zhuceButton addTarget:self action:@selector(zhuceButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.zhuceButton];
    
    [self.zhuceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(yqmView.mas_bottom).with.offset(40);
        make.left.equalTo(self.view).with.offset(40);
        make.right.equalTo(self.view).with.offset(-40);
        
    }];
    
    UILabel * dengluLabel = [[UILabel alloc] init];
    [self.view addSubview:dengluLabel];
    
    UIButton * dengluButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:dengluButton];
    
    [dengluLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).with.offset(-kSystemNavHeight);
        make.left.equalTo(self.view).with.offset(40);
        make.right.equalTo(self.view).with.offset(-self.view.bounds.size.width/2);
        
    }];
    
    dengluLabel.text = @"已有账户 ?";
    dengluLabel.font = [UIFont systemFontOfSize:15.f];
    dengluLabel.textAlignment= NSTextAlignmentRight;
    
    
    
    [dengluButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(dengluLabel);
        make.left.equalTo(dengluLabel.mas_right).with.offset(10);
        
    }];
    
    self.phoneText.delegate = self;
    self.yzmText.delegate = self;
    self.passWordText.delegate = self;
    self.QrPassWordText.delegate = self;
    self.yqmText.delegate = self;
    [dengluButton setTitle:@"立即登录" forState:UIControlStateNormal];
    dengluButton.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [dengluButton setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
    [dengluButton addTarget:self action:@selector(dengluButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)yzmButtonAction:(EBCountDownButton *)sender {
    
    if (self.phoneText.text.length == 0) {
        self.hud=[WKProgressHUD popMessage:@"手机号不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.phoneText.text.length != 11) {
        self.hud=[WKProgressHUD popMessage:@"请输入正确的手机号" inView:self.view duration:1 animated:YES];
        return;
    }
    [self.yzmButton countDownWithDuration:180 completion:^(BOOL finished) {
        NSLog(@"finished");
    }];
    self.para = @{@"phone" : self.phoneText.text,
                  @"type" : @(1)
                  };
    
    [GPosAPI requestSendCodeWithParams:self.para andCallback:^(id obj) {
        NSLog(@"--%@",obj);
        if ( obj == nil )
        {
            
            self.hud=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1.5 animated:YES];
            
            
        }else{
            
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                
                self.hud=[WKProgressHUD popMessage:@"发送短信验证码成功" inView:self.view duration:1.5 animated:YES];
               
            }else {
                
                self.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1.5 animated:YES];
               
            }
        
        }
       
        
    }];
    
    
}

//注册
- (void)zhuceButtonAction:(UIButton *)sender {
    
    if (self.phoneText.text.length == 0) {
         self.hud=[WKProgressHUD popMessage:@"注册手机号不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.yzmText.text.length == 0) {
        self.hud=[WKProgressHUD popMessage:@"验证码不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.passWordText.text.length == 0) {
        self.hud=[WKProgressHUD popMessage:@"密码不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.QrPassWordText.text.length == 0) {
        self.hud=[WKProgressHUD popMessage:@"确认密码不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.yqmText.text.length == 0) {
        self.hud=[WKProgressHUD popMessage:@"邀请码不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    
    
    
    if (self.phoneText.text.length != 11) {
        self.hud=[WKProgressHUD popMessage:@"请输入正确手机号格式" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.yzmText.text.length != 6) {
        self.hud=[WKProgressHUD popMessage:@"请输入正确验证码" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.passWordText.text.length < 8 ) {
        self.hud=[WKProgressHUD popMessage:@"密码请输入8~15个长度字符" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.passWordText.text.length > 15 ) {
        self.hud=[WKProgressHUD popMessage:@"密码请输入8~15个长度字符" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.QrPassWordText.text.length != self.passWordText.text.length) {
        self.hud=[WKProgressHUD popMessage:@"两次密码输入不相同" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.yqmText.text.length != 11) {
        self.hud=[WKProgressHUD popMessage:@"邀请人手机号格式不正确" inView:self.view duration:1 animated:YES];
        return;
    }
    

    //注册
    NSDictionary * params = @{@"phone" : self.phoneText.text,
                              @"verifyCode" : self.yzmText.text,
                              @"firstPassword" : self.passWordText.text,
                              @"secondPassword" : self.QrPassWordText.text,
                              @"inviterId" : self.yqmText.text
                              };
    self.zhuceButton.userInteractionEnabled=NO;
    [GPosAPI requestRegisterWithParams:params andCallback:^(id obj) {
        
        if ( obj == nil )
        {
             self.zhuceButton.userInteractionEnabled=YES;
            self.hud=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1 animated:YES];
            
        }else{
            
            if ([[obj objectForKey:@"code"] integerValue] == 200)
            {
                self.zhuceButton.userInteractionEnabled=YES;
                
                NSDictionary *dataDic = [NSDictionary dictionaryWithObject:self.phoneText.text forKey:@"info"];
                //注册通知
                NSNotification *LoseResponse = [NSNotification notificationWithName:@"buttonLoseRespons" object:nil userInfo:dataDic];
                
                //                2.发送通知:
                [[NSNotificationCenter defaultCenter] postNotification:LoseResponse];
                [self.navigationController popViewControllerAnimated:YES];
                
//                self.hud=[WKProgressHUD popMessage:@"注册成功" inView:self.view duration:1.5 animated:YES];
                [SVProgressHUD showInfoWithStatus:@"注册成功"];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else {
                
                 self.zhuceButton.userInteractionEnabled=YES;
                self.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1.5 animated:YES];
            }
            
        }
        
    }];
    


    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.phoneText ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.phoneText.text.length >= 11) {
            self.phoneText.text = [textField.text substringToIndex:11];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.phoneText.isSecureTextEntry ==YES)
        {
            self.phoneText.text=allstr;
            return NO;
        }
    }
    
    if (textField == self.yzmText ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.yzmText.text.length >= 6) {
            self.yzmText.text = [textField.text substringToIndex:6];
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.yzmText.isSecureTextEntry ==YES)
        {
            self.yzmText.text=allstr;
            return NO;
        }
    }
    
    if (textField == self.passWordText ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.passWordText.text.length >= 15) {
            self.passWordText.text = [textField.text substringToIndex:15];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.passWordText.isSecureTextEntry ==YES)
        {
            self.passWordText.text=allstr;
            return NO;
        }
    }
    
    if (textField == self.QrPassWordText ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.QrPassWordText.text.length >= 15) {
            self.QrPassWordText.text = [textField.text substringToIndex:15];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.QrPassWordText.isSecureTextEntry ==YES)
        {
            self.QrPassWordText.text=allstr;
            return NO;
        }
    }
    
    if (textField == self.yqmText ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.yqmText.text.length >= 11) {
            self.yqmText.text = [textField.text substringToIndex:11];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.yqmText.isSecureTextEntry ==YES)
        {
            self.yqmText.text=allstr;
            return NO;
        }
    }
    
    return YES;
}

//fanhui
- (void)dengluButtonAction:(UIButton *)sender {
    
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
